﻿
using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using YuanCode.Control.Controls;

namespace HuiZe.education.View
{
    /// <summary>
    /// SelectChapterView.xaml 的交互逻辑
    /// </summary>
    public partial class SelectChapterView : HuiZeWindow
    {
        private int chapterId { get; set; }
        private int courseId { get; set; }

        public SelectChapterView()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height + 10;
            this.MaxWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width + 10;
            Loaded += ResourceView_Loaded;
            PART_Next.Click += PART_Next_Click;
        }

        void PART_Next_Click(object sender, RoutedEventArgs e)
        {
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            if (string.IsNullOrEmpty(planLessoniewModel.class_id))
            {
                ShowDialogView _tip = new ShowDialogView();
                _tip.ShowDialogContent = "请选择课程";
                _tip.ShowDialog();
                return;
            }

            if (!planLessoniewModel.checkResource(this.chapterId, this.courseId))
            {
                ShowDialogView _tip = new ShowDialogView();
                _tip.ShowDialogContent = "当前资源未授权，请联系管理员";
                _tip.ShowDialog();
                return;
            }

            this.DialogResult = new bool?(true);
            planLessoniewModel.NextCommand.Execute(null);
        }

        private void ResourceView_Loaded(object sender, RoutedEventArgs e)
        {
            _dic_rb = new Dictionary<int, RadioButton>();
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            planLessoniewModel.selectChapterView = this;
            planLessoniewModel.GetzjSourceList();

            if (planLessoniewModel.TreeNodes2 != null && planLessoniewModel.TreeNodes2.Count > 0)
            {
                new Thread(new ThreadStart(() =>
                {
                    Thread.Sleep(500);
                    foreach (var _model in planLessoniewModel.TreeNodes2)
                    {
                        if (_model.ChildNodes != null && _model.ChildNodes.Count > 0)
                        {
                            foreach (var _node in _model.ChildNodes)
                            {
                                if (_dic_rb.ContainsKey(_node.NodeID))
                                {
                                    Dispatcher.Invoke(new Action(() =>
                                    {
                                        _dic_rb[_node.NodeID].IsChecked = new bool?(true);
                                    }));
                                    return;
                                }
                            }
                        }
                    }
                }))
                { IsBackground = true }.Start();
            }

        }
        string parentName { get; set; }

        void PART_Item_Checked(object sender, RoutedEventArgs e)
        {
            Select(((FrameworkElement)sender).DataContext as TreeModel);
        }

        void SourceTree_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //if (SourceTree.SelectedItem != null)
            //{
            //    Select(SourceTree.SelectedItem as TreeModel);
            //}
        }

        void Select(TreeModel selectedTnh)
        {
            if (selectedTnh == null) return;

            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            if (planLessoniewModel.TreeNodes2 != null && planLessoniewModel.TreeNodes2.Count > 0)
            {
                foreach (var _model in planLessoniewModel.TreeNodes2)
                {
                    if (selectedTnh.ParentID == _model.NodeID)
                    {
                        parentName = _model.NodeName;
                        break;
                    }
                }
            }
            planLessoniewModel.BKTitle = parentName + " " + selectedTnh.NodeName;

            //if (selectedTnh.ParentID == 0)
            //{
            //    parentName = selectedTnh.NodeName;
            //}
            //else
            //{
            //    planLessoniewModel.BKTitle = parentName + " " + selectedTnh.NodeName;
            //}

            planLessoniewModel.class_id = string.Empty;
            var list = planLessoniewModel.GetzjDirList(selectedTnh.NodeID);
            if (list != null && list.Count > 0)
            {
                foreach (var _item in list)
                {
                    if (_item.dir_item != null)
                    {
                        foreach (var _child_item in _item.dir_item)
                        {
                            _child_item._parent = _item;
                        }
                    }
                }
            }
            PART_Chapters.ItemsSource = list;

            //CheckList checkList = new CheckList();
            //ObservableCollection<CheckListItem> ckitemlist = new ObservableCollection<CheckListItem>();
            //var list = planLessoniewModel.GetzjDirList(selectedTnh.NodeID);
            //if (list != null && list.Count > 0)
            //{
            //    ZjWrap.Children.Clear();
            //    //ResroucesItem.Children.Clear();
            //    foreach (var item in list)
            //    {
            //        StackPanel stack2 = new StackPanel
            //        {
            //            Width = 980,
            //            Height = 40,
            //            HorizontalAlignment = HorizontalAlignment.Center,
            //            VerticalAlignment = VerticalAlignment.Top,
            //            Orientation = Orientation.Horizontal,
            //        };
            //        Label lb = new Label
            //        {
            //            Height = 40,
            //            //Width = 100,
            //            Content = item.chapter_name,
            //            Margin = new Thickness(3, 0, 0, 0),
            //            FontWeight = FontWeights.Bold,
            //            FontSize = 24,
            //            HorizontalAlignment = HorizontalAlignment.Left,
            //            VerticalAlignment = VerticalAlignment.Top,
            //        };
            //        GridSplitter gsp = new GridSplitter
            //        {
            //            Height = 40,
            //        };

            //        WrapPanel stack = new WrapPanel
            //        {
            //            Width = 880,
            //            Height = 40,
            //            HorizontalAlignment = HorizontalAlignment.Center,
            //            VerticalAlignment = VerticalAlignment.Top,
            //            Orientation = Orientation.Horizontal,
            //        };

            //        foreach (var dir in item.dir_item)
            //        {
            //            CustomRadioButton radioButton = new CustomRadioButton
            //            {
            //                Width = 100,
            //                Margin = new Thickness(10, 5, 0, 0),
            //                HorizontalAlignment = HorizontalAlignment.Left,
            //                Content = dir.dir_name,
            //                CornerRadius = new CornerRadius(6),
            //                FontSize = 16,
            //                Height = 30,
            //                ItemId = item.resources_kind_item_id.ToString(),
            //                //IsChecked = true,
            //                NomalBackground = Brushes.White,
            //                NomalForground = Brushes.Black,
            //                SelectedBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55E4C2")),
            //                SelectedForground = Brushes.White,
            //                Template = (ControlTemplate)FindResource("backRadioTemplate"),
            //                //
            //            };

            //            //CustomRadioButton radioButton = new CustomRadioButton
            //            //{
            //            //    //Width = 150,
            //            //    Height = 40,
            //            //    Margin = new Thickness(20, 0, 0, 0),
            //            //    HorizontalAlignment = HorizontalAlignment.Left,
            //            //    Content = dir.dir_name,
            //            //    CornerRadius = new CornerRadius(6),
            //            //    FontSize = 16,
            //            //    //IsChecked = true,
            //            //    NomalBackground = Brushes.White,
            //            //    NomalForground = Brushes.Black,
            //            //    SelectedBackground = Brushes.White,
            //            //    SelectedForground = Brushes.Blue,
            //            //    Template = (ControlTemplate)FindResource("backRadioTemplate"),
            //            //    //Template = (ControlTemplate)FindResource("selectedImageTemplate"),
            //            //    ItemId = item.resources_kind_item_id.ToString(),
            //            //};
            //            radioButton.Click += CustomRadioButton_Click_1;//CustomRadioButton_Click_1;

            //            //stack.Children.Add(lb);
            //            //stack.Children.Add(gsp);
            //            stack.Children.Add(radioButton);
            //        }

            //        stack2.Children.Add(lb);
            //        stack2.Children.Add(gsp);
            //        stack2.Children.Add(stack);
            //        ZjWrap.Children.Add(stack2);
            //    }
            //}
        }

        //private void CustomRadioButton_Click(object sender, RoutedEventArgs e)
        //{
        //    PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;

        //    CustomRadioButton radioButton = sender as CustomRadioButton;

        //    if (radioButton.IsChecked == true)
        //    {
        //        string type = radioButton.Content == null ? "" : radioButton.Content.ToString();
        //         var list= planLessoniewModel.GetResourcesCourseList(type);

        //        foreach (var item in list)
        //        {

        //            WrapPanel stack = new WrapPanel
        //            {
        //                Width = 253,
        //                Height = 30,
        //                HorizontalAlignment=HorizontalAlignment.Center,
        //                VerticalAlignment = VerticalAlignment.Top,
        //                Orientation = Orientation.Horizontal,
        //            };
        //            YCCheckBox chk = new YCCheckBox
        //            {
        //                Content = item.resources_name,
        //                Height = 30,
        //                Width = 150,
        //                FontSize = 24,
        //                HorizontalAlignment = HorizontalAlignment.Left,
        //                VerticalAlignment = VerticalAlignment.Top,
        //                Margin = new Thickness(0, 0, 0, 0),
        //                //Template = (ControlTemplate)FindResource("YCCheckBoxTemplate"),
        //                BorderBrush = Brushes.Cyan,
        //                ImageHeight = 16,
        //                IsChecked = false,
        //                ImageWidht = 16,
        //                NomalImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/chk_no.png")),
        //                SelectedImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/记住密码.png")),
        //                YCBorderThickness = new Thickness(2),
        //            };
        //            //Label lb = new Label
        //            //{
        //            //    Height = 30,
        //            //    Width = 80,
        //            //    Content = item.res_type_text,
        //            //    Margin = new Thickness(3, 0, 0, 0),
        //            //    HorizontalAlignment = HorizontalAlignment.Left,
        //            //    VerticalAlignment = VerticalAlignment.Top,
        //            //};
        //            stack.Children.Add(chk);
        //            //stack.Children.Add(lb);
        //            //ResroucesItem.Children.Add(stack);
        //            ChapterDock.Children.Add(stack);
        //        }
        //    }
        //}

        private void close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void CustomRadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            CustomRadioButton btn = sender as CustomRadioButton;
            string type = btn.Content.ToString();
            string class_id = btn.ItemId.ToString();
            planLessoniewModel.dir_name = type;

            planLessoniewModel.BkName = parentName + " " + type;

            planLessoniewModel.class_id = class_id;

        }

        void PART_ChapterItem_Checked(object sender, RoutedEventArgs e)
        {
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            resCourseDirChildModel _model = ((FrameworkElement)sender).DataContext as resCourseDirChildModel;

            planLessoniewModel.dir_name = _model.dir_name;
            planLessoniewModel.BkName = parentName + " " + _model.dir_name;
            planLessoniewModel.class_id = _model._parent.resources_kind_item_id.ToString();

            this.chapterId = _model.kind_item_chapter_id;
            this.courseId = _model.kind_item_course_id;
        }

        void RadioButton_Loaded(object sender, RoutedEventArgs e)
        {
            RadioButton _rb = (RadioButton)sender;
            TreeModel _model = _rb.DataContext as TreeModel;
            if (_model == null) return;

            if (!_dic_rb.ContainsKey(_model.NodeID))
                _dic_rb.Add(_model.NodeID, _rb);
        }

        Dictionary<int, RadioButton> _dic_rb;
    }
}
